package com.manxi.mall.controller;  // 修改包路径

import com.manxi.mall.bean.TradeProvinceOrderAmount;  // 修改包路径
import com.manxi.mall.service.TradeStatsService;  // 修改包路径
import com.manxi.mall.util.DateFormatUtil;  // 修改包路径
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Addroc
 * @date 2024/6/14
 * 交易域统计Controller
 */
@RestController
@RequestMapping("/trade")
public class TradeStatsController {

    @Autowired
    private TradeStatsService tradeStatsService;

    /**
     * 获取GMV（总交易额）/trade/gmv
     * @param date 日期参数，默认为0表示当天
     * @param region 区域参数，默认为"all"表示所有区域
     * @param category 类别参数，默认为"all"表示所有类别
     * @return 返回GMV的JSON格式数据
     */
    @RequestMapping("/gmv")
    public String getGMV(
        @RequestParam(value = "date", defaultValue = "0") Integer date,
        // @RequestParam(value = "region", defaultValue = "all") String region,
        // @RequestParam(value = "category", defaultValue = "all") String category
    ) {
        if (date == 0) {
            // 如果未传递日期参数，则使用当天日期
            date = DateFormatUtil.now();
        }
        BigDecimal gmv = tradeStatsService.getGMV(date);
        String json = "{\n" +
                "\t\t  \"status\": 0,\n" +
                "\t\t  \"data\": " + gmv + "\n" +
                "\t\t}";
        return json;
    }

    /**
     * 获取各省份交易额/trade/province
     * @param date 日期参数，默认为0表示当天
     * @return 返回各省份交易额的JSON格式数据
     */
    @RequestMapping("/province")
    public String getProvinceAmount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderAmount> provinceOrderAmountList = tradeStatsService.getProvinceAmount(date);

        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < provinceOrderAmountList.size(); i++) {
            TradeProvinceOrderAmount provinceOrderAmount = provinceOrderAmountList.get(i);
            jsonB.append("{\"name\": \"" + provinceOrderAmount.getProvinceName() + "\",\"value\": " + provinceOrderAmount.getOrderAmount() + "}");
            if (i < provinceOrderAmountList.size() - 1) {
                jsonB.append(",");
            }
        }

        jsonB.append("],\"valueName\": \"交易额\"}}");
        return jsonB.toString();
    }

    /**
     * 获取订单数量/trade/orderCount
     * @param date 日期参数，默认为0表示当天
     * @return 返回订单数量的JSON格式数据
     */
    @RequestMapping("/orderCount")
    public String getOrderCount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        Integer orderCount = tradeStatsService.getOrderCount(date);
        String json = "{\n" +
                "\t\t  \"status\": 0,\n" +
                "\t\t  \"data\": " + orderCount + "\n" +
                "\t\t}";
        return json;
    }

    /**
     * 获取用户数量/trade/userCount
     * @param date 日期参数，默认为0表示当天
     * @return 返回用户数量的JSON格式数据
     */
    @RequestMapping("/userCount")
    public String getUserCount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        Integer userCount = tradeStatsService.getUserCount(date);
        String json = "{\n" +
                "\t\t  \"status\": 0,\n" +
                "\t\t  \"data\": " + userCount + "\n" +
                "\t\t}";
        return json;
    }
}